#include "air780e_filesystem.h"

// 外部flash文件系统






// -------内部文件系统

/* file system support */
FILE* pika_platform_fopen(const char* filename, const char* modes) {
    return luat_fs_fopen(filename, modes);
}

int pika_platform_fclose(FILE* stream) {
    return luat_fs_fclose(stream);
}

size_t pika_platform_fwrite(const void* ptr,
                                      size_t size,
                                      size_t n,
                                      FILE* stream) {
    pika_assert(NULL != stream);
    return luat_fs_fwrite(ptr, size, n, stream);
}

size_t pika_platform_fread(void* ptr,
                                     size_t size,
                                     size_t n,
                                     FILE* stream) {
    return luat_fs_fread(ptr, size, n, stream);
}

int pika_platform_fseek(FILE* stream, long offset, int whence) {
    return luat_fs_fseek(stream, offset, whence);
}

long pika_platform_ftell(FILE* stream) {
    return luat_fs_ftell(stream);
}




//列出目录文件
char** pika_platform_listdir(const char* path, int* count){
    
    #if 0
    struct dirent* dp;
    DIR* dir = opendir(path);

    char** filenames = NULL;
    *count = 0;

    if (dir != NULL) {
        while ((dp = readdir(dir)) != NULL) {
            if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
                size_t size_old = (*count) * sizeof(char*);
                size_t size_new = (size_old) + sizeof(char*);
                filenames =
                    (char**)pika_reallocn(filenames, size_old, size_new);
                filenames[*count] = pika_platform_strdup(dp->d_name);
                (*count)++;
            }
        }
        closedir(dir);
    }
    return filenames;
    #endif
    const uint8_t MAX_NUM = 64;

    const char* dir_path = path;
    char** filenames = NULL;
    *count = 0;
    
    luat_fs_dirent_t *fs_dirent = LUAT_MEM_MALLOC(sizeof(luat_fs_dirent_t)*MAX_NUM);
    memset(fs_dirent, 0, sizeof(luat_fs_dirent_t)*MAX_NUM);

    int lsdir_cnt = luat_fs_lsdir(dir_path, fs_dirent, 0, MAX_NUM);

    if (lsdir_cnt > 0)
    {
        // char path[255] = {0};

        // LUAT_DEBUG_PRINT("dir_path=%s, lsdir_cnt=%d", dir_path, lsdir_cnt);

        for (size_t i = 0; i < lsdir_cnt; i++)
        {
            // memset(path, 0, sizeof(path));            

            size_t size_old = (*count) * sizeof(char*);
            size_t size_new = (size_old) + sizeof(char*);
            filenames =
                    (char**)pika_reallocn(filenames, size_old, size_new);
                filenames[*count] = pika_platform_strdup((fs_dirent+i)->d_name);
                (*count)++;

            #if 0
            switch ((fs_dirent+i)->d_type)
            {
            // 文件类型
            case 0:   //文件
                // snprintf(path, sizeof(path)-1, "%s%s", dir_path, (fs_dirent+i)->d_name);             
                // LUAT_DEBUG_PRINT("\tfile=%s, size=%d", path, luat_fs_fsize(path));

                // LUAT_DEBUG_PRINT("file= %s", (fs_dirent+i)->d_name);

                break;
            case 1:  //目录
                snprintf(path, sizeof(path)-1, "%s%s/", dir_path, (fs_dirent+i)->d_name);
                // recur_fs(path); //不递归
                
                break;

            default:
                break;
            }
            #endif
        }        
    }

    LUAT_MEM_FREE(fs_dirent);
    fs_dirent = NULL;
    
    return filenames;
    
}





int pika_platform_rmdir(const char* pathname) {
    return luat_fs_rmdir(pathname);
}

int pika_platform_mkdir(const char* pathname, int mode) {
    int res = luat_fs_mkdir(pathname);
    pika_platform_printf("pathname:%s, mode:%d, res:%d\n", pathname, mode, res);
    return res;
}

int pika_platform_path_exists(const char* path) {
    if (luat_fs_fexist(path)){
        return 1;
    }
    if (luat_fs_dexist(path)){
        return 1;
    }
    return 0;
}

int pika_platform_path_isdir(const char* path) {
    return luat_fs_dexist(path);
}

int pika_platform_path_isfile(const char* path) {
    return luat_fs_fexist(path);
}

int pika_platform_remove(const char* pathname) {
    return luat_fs_remove(pathname);
}


int pika_platform_rename(const char* oldpath, const char* newpath) {
    return luat_fs_rename(oldpath, newpath);
}



char* pika_platform_getcwd(char* buf, size_t size) {
    // 无法实现
}

int pika_platform_chdir(const char* path) {
    // 无法实现
}

char* pika_platform_realpath(const char* path, char* resolved_path) {
    // 无法实现
}

